import * as DateUtil from '../../commons/DateUtil'
import StoreUtils from '../../commons/StoreUtils'
import * as Const from '../../commons/Const'
const { ChatType } = require('../../commons/ChatUtils')

export default {
  // 错误提示
  openMessage(error) {
    this.$Message.error(error);
  },

  // 显示表情框
  showFaceBox: function () {
    this.showFace = !this.showFace;
  },

  // 选择表情插入到消息输入区域
  insertFace: function (item) {
    this.messageContent = this.messageContent + 'face' + item;
    this.showFace = false;
  },

  // 本人发送信息
  mineSend() {
    let self = this;
    let time = new Date().getTime();
    let content = self.messageContent;

    if (content !== '' && content !== '\n') {
      if (content.length > 2000) {
        self.openMessage('不能超过2000个字符');

      } else {
        let currentMessage = {
          type: ChatType.MSG_USER,
          groupId: StoreUtils.getValue(Const.CONF_ID),
          username: StoreUtils.getValue(Const.USERNAME),
          content: self.messageContent,
          timestamp: time
        };

        // 发送消息
        self.send(currentMessage);
      }
    }
  },

  // 发送消息的基础方法
  send(message) {
    // 向服务器发送消息
    this.$store.commit('sendMessage', message);

    // 清空消息发送区域
    this.messageContent = '';
  },

  // 是否为系统消息
  systemMsg: function (msgType) {
    return msgType === ChatType.MSG_SYSTEM
  }
}
